import {api} from './libs/api.js';
import {member} from '@/api/member.js';
let _self;
export const index =  {
	data() {
		return {
			showForm:false,
			showCodeBox:false,
			showAccount:false,
			showAgree:false,
			agreeChecked:"否",
			optLock:false,
			picker:{},
			checkMap:{
				"sex":api.sex
			},
			userInfo:{"id":0},
			agree:api.agree,
			rules:api.rules,
			formData:api.formData,
			formField:[]
		}
	},
	
	mounted() {
		_self = this;
		this.initData();
	},
	
	methods: {
		async initData(){
			
			let userToken =  uni.getStorageSync('USER_TOKEN');//检查登录状态
			if(userToken){
				this.userInfo = await api.getMember({"token":userToken});
			}else{
				this.showCodeBox=true;
			}

			if(this.userInfo&&this.userInfo["id"]){
				this.showCodeBox=false;
				this.showForm=true;
				this.formField=await api.formField({});
				this.formData["id"]=this.userInfo["id"];
			}else{
				this.showCodeBox=true;
			}
			
			if(this.userInfo["id"]&&!this.userInfo["account_num"]){
				this.showForm=false;
				this.showAccount=true;
			}
			
			
			//表单赋值
			if(this.formField.length>0){
				for(let index in this.formField){
					let item=this.formField[index];
					this.formData[item.name]=this.userInfo[item.name];
				}
				
				this.initPicker();
			}
			
			console.log(this.formField);
			
			console.log(this.formData);
		},
		
		async submitForm(form){
			if(_self.optLock) return true;
			_self.optLock=true;
			
			if(this.agreeChecked!="是"){
				uni.showToast({icon:"none",title:"请勾选《用户隐私保护协议》！"});
				_self.optLock=false;
				return true;
			}
			
			
			this.$refs.form.validate().then(res=>{
				let userToken =  uni.getStorageSync('USER_TOKEN');
				res["token"]=userToken;
				res["platform"]=_self.$store.state.user.loginProvider;

				if(res["agree"]) delete res["agree"];
				if(_self.formData["id"]) res["id"]=_self.formData["id"];
				if(_self.formData["account_num"]) res["account_num"]=_self.formData["account_num"];
				
				//console.log(res);
				_self.userOauth(res);
			}).catch(err =>{
				_self.optLock=false;
				console.log('表单错误信息：', err);
			})
		},
		
		async userOauth(res){
			let ret= await api.userOauth(res);
			if(ret){
				uni.showToast({icon:"none",title:"操作成功！"});
				setTimeout(function(){uni.reLaunch({url:"/pages/index/index"});},2000);　　
			}else{
				_self.optLock=false;
			}

		},
		
		async confirmCode(){
			let userToken =  uni.getStorageSync('USER_TOKEN');
			let retInfo=await api.checkUserCode({"user_code":this.formData.user_code,"token":userToken});
			if(retInfo){
				if(retInfo["user_info"]){
					this.member=retInfo["user_info"];
					uni.setStorageSync('MEMBER_INFO',retInfo["user_info"]);
					this.$store.commit("setMemberInfo",retInfo["user_info"]);
					uni.reLaunch({url:"/pages/index/index"});
				}else{
					this.showCodeBox=false;
					this.showForm=true;
					this.formField=retInfo;
					this.initPicker();
				}
				
			}
		},
		
		async confirmAccount(){
			if(!this.formData["id"]) return false;
			//let res=Object.assign({"token":uni.getStorageSync('USER_TOKEN')},this.formData);
			let retInfo=await api.updateUser({"account_num":this.formData["account_num"]});
			uni.setStorageSync('MEMBER_INFO',retInfo);
			this.$store.commit("setMemberInfo",retInfo);
			uni.reLaunch({url:"/pages/index/index"});
		},
		
		
		
		//初始化picker
		initPicker(){
			let indexList={};
			for(let index in this.formField){
				let item=this.formField[index];
				if(item.field_type=="picker"){
					indexList[item.name]={"index":0,"items":item.data_list};
					if(this.formData[item.name]){
						for(let i in item.data_list){
							if(item.data_list[i]["value"]==this.formData[item.name]) indexList[item.name]["index"] = i; 
						}
					}else{
						this.formData[item.name]=item.data_list[0].value;
					}
					
				} 
			}
			this.picker=indexList;
		},
		
		selectChange: function(e) {
			let key=e.currentTarget.dataset["key"];
			let index=e.detail.value;
		    this.picker[key]["index"] =index;
			this.formData[key]=this.picker[key]["items"][index]["value"];
		},
		
		agreeClick(res){
			this.showAgree=true;
		}
	},
}